package com.sims.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sims.service.IUserService;
import com.sims.dao.ISimsStudentDao;
import com.sims.eo.SimsStudent;
import com.sims.util.HibernateSessionFactory;

public class SimsStudentDaoImp implements ISimsStudentDao {

	@Override
	public SimsStudent getStudentByStudentNumber(String studentNumber) {
		// 从数据库获取对象
		SimsStudent simsStudent = null;
		String sql = "from SimsStudent s where s.studentNumber=?";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setParameter(0, studentNumber);
		List students = query.list();
		if (students.size() != 0) {
			simsStudent = (SimsStudent) students.get(0);
		}
		HibernateSessionFactory.closeSession();
		return simsStudent;
	}

	@Override
	public List<SimsStudent> getAllSimsStudent() {
		String sql = "from SimsStudent";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		List<SimsStudent> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public boolean deletSimsStudentById(Long id) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 查询删除对象
			SimsStudent student = (SimsStudent) session.get(SimsStudent.class,
					id);
			// 持久化操作
			session.delete(student);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public SimsStudent getSimsStudentById(Long id) {
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 查询删除对象
		SimsStudent student = (SimsStudent) session.get(SimsStudent.class, id);
		return student;
	}

	@Override
	public boolean updateSimsStudent(SimsStudent student) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.update(student);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public List<SimsStudent> getAllSimsStudent(int pageNo, int pageSize) {
		String sql = "from SimsStudent";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<SimsStudent> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public long getRowCount() {
		String hql = "select count(id) from SimsStudent";
		long count = 0;
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long getRowCountByStudentNumber(String studentNumber) {
		String hql = "select count(studentNumber) from SimsStudent s where s.studentNumber=?";
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		query.setParameter(0, studentNumber);
		long count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long createStudent(SimsStudent student) {
		Long id = 0L;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.save(student);
			// 提交事务
			trans.commit();
			id = student.getId();
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return id;
	}

}
